package cn.bluarry.donationuser.service.impl;

import cn.bluarry.donationuser.Respose.UserResponse;
import cn.bluarry.donationuser.Respose.ListUsersResponse;
import cn.bluarry.donationuser.mapper.UserMapper;
import cn.bluarry.donationuser.pojo.Donation_User;
import cn.bluarry.donationuser.service.IUserService;
import cn.bluarry.donationuser.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("iUserService")
public class UserServiceiml implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserResponse<Donation_User> CreateUser(HttpServletRequest request, String requestid, Date currentdate) {
        String UserName=request.getParameter("UserName");


        if(StringUtils.isBlank(UserName)){
            return UserResponse.createByerrorMsg("用户名不能为空");
        }else if(!checkValid(UserName)){
            return UserResponse.createByerrorMsg("用户名已存在");
        }

        Map<String,String> map=new HashMap<String, String>();
        map.put("UserName",UserName);
        map.put("UserId", Utils.getUUID());
        map.put("DisplayName",request.getParameter("DisplayName"));
        map.put("Email",request.getParameter("Email"));
        map.put("MobilePhone",request.getParameter("MobilePhone"));
        map.put("Comments",request.getParameter("Comments"));
        Donation_User donationUser =new Donation_User(map,currentdate);

        int count=userMapper.insert(donationUser);
        if(count > 0){
            return  new UserResponse<>(requestid,donationUser);
        }
        return  UserResponse.createByerrorMsg("注册失败");
    }

    /**
     * 检查用户是否唯一存在
     * @param UserName
     * @return
     */
    public boolean checkValid(String UserName){
        int c=userMapper.checkUsername(UserName);
        if(c>0){
            return  false;
        }
        return true;
    }

    @Override
    public ListUsersResponse<Donation_User> ListUsers(HttpServletRequest request, String requestid) {

        List<Donation_User> list=userMapper.ListUsers();
        if(list!=null){
            return new ListUsersResponse<>(requestid,list);
        }
        return ListUsersResponse.createByerrorMsg("查询失败");
    }

    @Override
    public UserResponse<Donation_User> UpdateUser(HttpServletRequest request, String requestid, Date currentdate) {
        String UserName=request.getParameter("UserName");

        if(StringUtils.isBlank(UserName)){
            return UserResponse.createByerrorMsg("用户名不能为空");
        }else if(checkValid(UserName)){
            return UserResponse.createByerrorMsg("用户名不存在");
        }
        String NewUserName=request.getParameter("NewUserName");
        String NewMobilePhone=request.getParameter("NewMobilePhone");
        String NewDisplayName=request.getParameter("NewDisplayName");
        String NewEmail=request.getParameter("NewEmail");
        String NewComments=request.getParameter("NewComments");
        Donation_User u=userMapper.getUserInfoByUsername(UserName);
        int c = userMapper.updateUserInfo(UserName,NewUserName,NewComments,NewMobilePhone,NewDisplayName,NewEmail,currentdate);
        if(c>0){
            if(StringUtils.isBlank(NewUserName)){
                u=userMapper.getUserInfoByUsername(UserName);
            }else{
                u=userMapper.getUserInfoByUsername(NewUserName);
            }
        }
        return new UserResponse<Donation_User>(requestid,u);
    }

    @Override
    public UserResponse<Donation_User> DeleteUser(HttpServletRequest request, String requestid) {
        String UserName=request.getParameter("UserName");

        if(StringUtils.isBlank(UserName)){
            return UserResponse.createByerrorMsg("用户名不能为空");
        }else if(checkValid(UserName)){
            return UserResponse.createByerrorMsg("用户名不存在");
        }
        userMapper.DeleteUserByName(UserName);

        return new UserResponse<Donation_User>(requestid);
    }


}
